﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MLM.Data;
using System.Drawing;
using MLM.AdminCP.Class;
using MLM.AdminCP;

namespace MLM.Home.Pages
{
    public partial class Checkout : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            BindData();
            CheckoutBtn.Click += new EventHandler(CheckoutBtn_Click);
       
        }

        public void BindData()
        {
            gvShoppingCart.DataSource = MLM.ShoppingCart.Instance.Items;
            gvShoppingCart.DataBind();
        }

        protected void gvShoppingCart_RowDataBound(object sender, GridViewRowEventArgs e)
        {

        }

        /**
         * Thực hiện xóa item trong giỏ hàng
         */
        protected void gvShoppingCart_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Remove")
            {
                int productId = Convert.ToInt32(e.CommandArgument);
                MLM.ShoppingCart.Instance.RemoveItem(productId);
            }

            // Hiển thị lại dữ liệu
            BindData();
        }
        //Thực hiện update giỏ hàng 
        protected void btnUpdateCart_Click(object sender, EventArgs e)
        {
            foreach (GridViewRow row in gvShoppingCart.Rows)
            {
                if (row.RowType == DataControlRowType.DataRow)
                {
                    try
                    {
                        // Lấy productId
                        int productId = Convert.ToInt32(gvShoppingCart.DataKeys[row.RowIndex].Value);
                        // Tìm và lấy giá trị của số lượng
                        int quantity = int.Parse(((TextBox)row.Cells[1].FindControl("txtQuantity")).Text);
                        MLM.ShoppingCart.Instance.SetItemQuantity(productId, quantity);
                    }
                    catch (FormatException) { }
                }
            }

            BindData();
        }
        void CheckoutBtn_Click(object sender, EventArgs e)
        {
            if (FirstNameTxt.Text == "" || LastNameTxt.Text == "" || AddressTxt.Text == "" || PostalCodeTxt.Text == "" || EmailTxt.Text == "" || PhoneNumberTxt.Text == "")
            {
                MessageLbl.Text = "Please correct your form!";
                MessageLbl.ForeColor = Color.Red;
            }
            else
            {
                try
                {
                    MLMDataContext ct = new MLMDataContext();
                    var userOrder = new UserOrder();
                    userOrder.Address = AddressTxt.Text;
                    userOrder.CityID = int.Parse(CityDDL.SelectedValue);
                    userOrder.CountryID = int.Parse(CountryDDL.SelectedValue);
                    userOrder.DateOfBirth = DateOfBirthPk.SelectedDate;
                    userOrder.DateTimeCreated = DateTime.Now;
                    userOrder.DepoID = 0; // sua cai nay cho depo vao
                    userOrder.Email = EmailTxt.Text;
                    userOrder.FirstName = FirstNameTxt.Text;
                    userOrder.LastName = LastNameTxt.Text;
                    userOrder.Gender = GenderDDL.SelectedValue == "1" ? true : false;
                    userOrder.PhoneNumber = PhoneNumberTxt.Text;
                    userOrder.PostalCode = int.Parse(PostalCodeTxt.Text);
                    userOrder.Status = 1;
                    ct.UserOrders.AddObject(userOrder);
                    if (ct.SaveChanges() > 0)
                    {
                        for (int i = 0; i < 1000; i++) //luu san pham vao
                        {
                            UserOrderDetail detail = new UserOrderDetail();
                            detail.UserOrderID = userOrder.UserOrderID;
                            //cac thong tin khac
                            ct.AddToUserOrderDetails(detail);
                        }
                        if (ct.SaveChanges() > 0)
                        {
                            btnPaymentPP.Visible = true;
                            MessageLbl.Text = "Checkout successfully! We will contact you as soon as posible!";
                            MessageLbl.ForeColor = Color.Green;
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageLbl.Text = "Has an error occurred! Please correct your form!";
                    MessageLbl.ForeColor = Color.Red;
                }   
            }            
        }
    }
}